/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 *						Doris Vaton <doris.vaton@gmail.com>
 * 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shaipicku.mjpegcams;

import java.util.Locale;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class StartActivity extends Activity {
	boolean DEBUG = true;
	private Dialog dialog;
	public Locale locale = null;
	private SharedPreferences sharedPrefs = null;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

		Configuration config = getBaseContext().getResources().getConfiguration();
		String lang = sharedPrefs.getString("languages", "");
		if (!"".equals(lang) &&! config.locale.getLanguage().equals(lang))
		{
			locale = new Locale(lang);
			Locale.setDefault(locale);
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		}

		setContentView(R.layout.start_activity_layout);
		
		LogCat.flushLog(this);
		Button camera = (Button) findViewById(R.id.cam_button);
		camera.setOnClickListener(new ClickListener_Button());
		Button shape = (Button) findViewById(R.id.shape_button);
		shape.setOnClickListener(new ClickListener_Button());
		Button color = (Button) findViewById(R.id.color_button);
		color.setOnClickListener(new ClickListener_Button());
		Button shape_color = (Button) findViewById(R.id.shape_color_button);
		shape_color.setOnClickListener(new ClickListener_Button());
		Button pile = (Button) findViewById(R.id.pile_button);
		pile.setOnClickListener(new ClickListener_Button());
		Button about = (Button) findViewById(R.id.about);
		about.setOnClickListener(new ClickListener_Button());
	}

	/** 
	 * Class defining what happened when the user clicks on a shape button 
	 */
	class ClickListener_Button implements Button.OnClickListener{
		@Override
		public void onClick(View button) {
			/** Check which button was selected and return the string of the shape selected **/
			Intent intent = null;
			Bundle bundle = null;
			switch(button.getId()){
			case R.id.cam_button:
				intent = new Intent(StartActivity.this, MjpegCamActivity.class);
				bundle = new Bundle();
				//Add the parameters to bundle
				bundle.putBoolean("DEBUG", DEBUG);
				bundle.putString("MODE", "CAMERA");
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
				break;

			case R.id.color_button:
				intent = new Intent(StartActivity.this, MjpegCamActivity.class);
				bundle = new Bundle();
				//Add the parameters to bundle
				bundle.putBoolean("DEBUG", DEBUG);
				bundle.putString("MODE", "COLOR");
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
				break;

			case R.id.shape_button:
				intent = new Intent(StartActivity.this, MjpegCamActivity.class);
				bundle = new Bundle();
				//Add the parameters to bundle
				bundle.putBoolean("DEBUG", DEBUG);
				bundle.putString("MODE", "SHAPE");
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
				break;

			case R.id.shape_color_button:
				intent = new Intent(StartActivity.this, MjpegCamActivity.class);
				bundle = new Bundle();
				//Add the parameters to bundle
				bundle.putBoolean("DEBUG", DEBUG);
				bundle.putString("MODE", "SHAPE_COLOR");
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
				break;

			case R.id.pile_button:
				intent = new Intent(StartActivity.this, MjpegCamActivity.class);
				bundle = new Bundle();
				//Add the parameters to bundle
				bundle.putBoolean("DEBUG", DEBUG);
				bundle.putString("MODE", "PILE_UP");
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
				break;

			case R.id.about:
				initiatePopupWindow();
				break;

			default :
				Log.i("Start", "No button recognized!\n");
				break;
			}  
		}
	}

	// If the drawer is open, pressing the key back will close it.
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			SlidingDrawer sDrawer = (SlidingDrawer) findViewById(R.id.drawer);
			if(sDrawer.isOpened() == true){
				sDrawer.animateClose();	
			}
			else{
				return super.onKeyDown(keyCode, event);
			}
		}return false;
	}


	/**
	 * Generate the menu.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// refresh the tabs and their content
		//    	Toast.makeText(RSSFeed.this, "Option", Toast.LENGTH_SHORT).show();
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}
	/**
	 * Listener of the menu.
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		Bundle bundle;
		switch (item.getItemId()) {
		case R.id.option:
			intent = new Intent(this, SettingsActivity.class);
			bundle = new Bundle();
			//Add the parameters to bundle
			bundle.putBoolean("DEBUG", DEBUG);
			intent.putExtras(bundle);
			startActivity(intent);
			finish();
			return true;
		case R.id.quit:
			finish();
			return true;
		case R.id.logcat:
			intent = new Intent(this, LogCat.class);
			startActivity(intent);
			return true;
		}
		return false;
	}

	public void onDestroy(){
		super.onDestroy();
		LogCat.flushLog(this);
	}

	private void initiatePopupWindow() {
		dialog=new Dialog(this);
		View view = View.inflate(StartActivity.this, R.layout.popuplayout, null);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setTitle(getString(R.string.about_title));
		TextView textView = (TextView) view.findViewById(R.id.aboutTv);
		textView.setMovementMethod(LinkMovementMethod.getInstance());
		dialog.show();
	}
}